/*
 * =====================================================================================
 *
 *       Filename:  test_log.cxx
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  04/29/2011 06:19:26 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Ken Chen (Ken), chen_kenz@emc.com
 *        Company:  EMC
 *
 * =====================================================================================
 */

#include <unistd.h>
#include <execinfo.h>
#include <iostream>
#include "cl_logger.hxx"
#include "cl_thread.hxx"
#include "cl_mutex.hxx"
#include "cl_signal_handler.hxx"
#include "cl_singleton.hxx"

using namespace std;
using namespace neo;

static CLLogger syncLogger("sync.log");
static CLLogger asyncLogger("async.log");
static bool done = false;

static void doLog(void)
{
  int i = 0;
  for(; !done;)
  {
    asyncLogger.log("LOG1", CLLogger::LOG_CRITICAL, "%s_%d", "test", ++i);
    syncLogger.log("LOG1", CLLogger::LOG_CRITICAL, "%s_%d", "test", i);
    sleep(1);
  }
}

static void doLog2(void)
{
  int i = 0;
  for(; !done;)
  {
    asyncLogger.log("LOG2", CLLogger::LOG_CRITICAL, "%s_%d", "test", ++i);
    syncLogger.log("LOG2", CLLogger::LOG_CRITICAL, "%s_%d", "test", i);
    sleep(1);
  }
}

static void setdone(void)
{
  done = true;
  exit(0);
}

static void violated(void)
{
	sleep(10);
//	int* p = NULL;
//	*p = 1000;
}

int main(int argc, char* argv[])
{
  CLSingleton<CLSignalHandler>::create();
  CLSingleton<CLSignalHandler>::instance()->installHandlerFor(SIGUSR2, bind(setdone));

  CLThread logThr1(bind(doLog), false);  
  CLThread logThr2(bind(doLog2), false);  
  CLThread vThr(bind(violated), false);  

  logThr1.start();
  logThr2.start();
  vThr.start();
  cout << "wait for signal\n";
	CLSingleton<CLSignalHandler>::instance()->waitForSignal();
}

